﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Perspective
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            char[] delimiters = new char[] { '\t', ' ' };
            DialogResult rez = this.openFileDialog1.ShowDialog();
            if (rez == DialogResult.OK)
            {
                rez = this.saveFileDialog1.ShowDialog();
                if (rez == DialogResult.OK)
                {
                    this.button1.Enabled = false;

                    bool removeFirst = this.checkBox1.Checked;
                    double scale;
                    Double.TryParse(this.textBox1.Text, out scale);
                    if (scale == 0) scale = 1;
                    StreamReader tr = new StreamReader(this.openFileDialog1.OpenFile());
                    StreamWriter tw = new StreamWriter(this.saveFileDialog1.OpenFile());

                    try
                    {
                        do
                        {
                            //string str = .Trim();
                            string str = System.Text.RegularExpressions.Regex.Replace(tr.ReadLine(), @"\s+", " ");
                            string[] args = str.Split(delimiters);

                            int dim = args.Length;
                            double tmp = 0;
                            int i = 0;

                            if (removeFirst) i++;

                            for (; i < dim; i++)
                            {
                                double.TryParse(args[i], out tmp);
                                tmp *= scale;
                                if (i != dim - 1)
                                    tw.Write(tmp + " ");
                                else
                                    tw.Write(tmp + "");
                            }
                            tw.Write("\n");


                        } while (tr.Peek() >= 0);

                    }
                    catch
                    {
                        Form a = new ErrorForm();
                        a.Show();
                    }
                    finally
                    {
                        tr.Close();
                        tw.Close();
                    }

                    this.button1.Enabled = true;
                }
            }
        }
    }
}
